import { _decorator, Component, Node, SpriteFrame, tween, Vec3, Animation, Sprite, Color, sp } from 'cc';
import { CELL_STATUS, CELL_WIDTH, CELL_HEIGHT, ANITIME, CELL_WIDTH_MARGIN } from './ConstValue';
const { ccclass, property } = _decorator;

@ccclass('CellView')
export class CellView extends Component {

    @property(SpriteFrame)
    private defaultFrame: SpriteFrame = null

    public isSelect: boolean
    public model
    public status

    start() {
        this.isSelect = false;
    }

    update(deltaTime: number) {

    }

    public initWithModel(model) {
        this.model = model;
        var x = model.startX;
        var y = model.startY;
        this.node.position = new Vec3(CELL_WIDTH * (x - 0.5) + CELL_WIDTH_MARGIN, CELL_HEIGHT * (y - 0.5), 0);
        if (model.status == CELL_STATUS.COLUMN || model.status == CELL_STATUS.LINE) {
            var animation = this.node.getComponent(Animation);
            animation.play();
        }
    }

    updateView() {
        const cmd = this.model.cmd;
        if (cmd.length <= 0) {
            return;
        }

        cmd.forEach((command) => {
            const executeCommand = () => {
                if (command.action === "moveTo") {
                    const x = ((command.pos.x - 0.5) * CELL_WIDTH) + CELL_WIDTH_MARGIN;
                    const y = (command.pos.y - 0.5) * CELL_HEIGHT;
                    tween(this.node)
                        .to(ANITIME.TOUCH_MOVE, { position: new Vec3(x, y) })
                        .start();
                } else if (command.action === "toDie") {
                    if (this.status === CELL_STATUS.BIRD) {
                        this.scheduleOnce(this.node.destroy, ANITIME.BOMB_BIRD_DELAY);
                    } else {
                        this.scheduleOnce(() => {
                            this.node.destroy();
                        }, ANITIME.BOMB_BIRD_DELAY);
                    }
                } else if (command.action === "setVisible") {
                    const isVisible = command.isVisible;
                    let sprite = this.node.getComponent(Sprite)
                    let curColor = sprite.color
                    let newColor = new Color(curColor.r, curColor.g, curColor.b, isVisible ? 255 : 0);
                    sprite.color = newColor;
                } else if (command.action === "toShake") {
                    // Shake动画例子
                    tween(this.node)
                        .by(0.06, { eulerAngles: new Vec3(0, 0, 30) })
                        .by(0.12, { eulerAngles: new Vec3(0, 0, -60) })
                        .by(0.06, { eulerAngles: new Vec3(0, 0, 30) })
                        .union() // 使用union使动画同时进行
                        .repeat(2) // 重复两次
                        .start();
                }
            };

            if (command.playTime > 0) {
                this.scheduleOnce(() => {
                    executeCommand();
                }, command.playTime);
            } else {
                executeCommand();
            }
        });
    }

    public setSelect(flag) {
        var bg = this.node.getChildByName("select");
        if (flag == false && this.isSelect && this.model.status == CELL_STATUS.COMMON) {
            this.node.getComponent(Sprite).spriteFrame = this.defaultFrame;
        }
        else if (flag && this.model.status == CELL_STATUS.COMMON) {
        }
        else if (flag && this.model.status == CELL_STATUS.BIRD) {
        }
        bg.active = flag;
        this.isSelect = flag;
    }
}


